Hi, I'm working on a program which is supposed to identify prime numbers and output prime pairs (prime numbers which are 1 integer value away from each other).
My function for checking prime numbers seems to be working correctly but i can't seem to make the program output the list of prime pairs. I'm guessing I set up the for loop wrong or the if else statements.
Any help is much appreciated!
Code:
/*-------------
Include Section
--------------*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
/*------------------Main Functions------------------
Purpose: Prompts user to input
Returns: Singal to the OS
--------------------------------------------------*/
int
main(void)
{
int code, userinput, i, positiveinteger;
printf("To generate prime pairs < 10000, enter 1\nTo check for a prime, enter 2\nTo terminate the program, enter 0: ");
scanf("%d", &code);
while (code != 0) {
if (code == 1) {
printf("\nEnter a positive integer > 5 and < 10000: ");
scanf("%d", &userinput);
printf("\nThe prime pairs up to %d are: \n", userinput);
for (i = 3; i <= userinput; i++) {
if (isprime(userinput) == TRUE && isprime (userinput + 2) == TRUE) {
printf("\n%*d", 5, isprime(userinput));
printf(" ,%*d\n", 5, isprime(userinput + 2));
}
}
}
else
if (code == 2) {
printf("\nEnter a positive integer > 5 and < 2,000,000: ");
scanf("%d", &positiveinteger);
if (isprime(positiveinteger) == TRUE)
printf("\n%d is a prime.\n\n", positiveinteger);
else
printf("\n%d is not a prime.\n\n", positiveinteger);
}
else
printf("\nWrong code.\n\n");
printf("To generate prime pairs < 10000, enter 1\nTo check for a prime, enter 2\nTo terminate the program, enter 0: ");
scanf("%d", &code);
}
printf("\n*** Program Terminated ***\n");
return (EXIT_SUCCESS);
}
/*------------------Prime Functions------------------
Purpose: Check if input is a prime number
Returns: True or False
--------------------------------------------------*/
int isprime(int userinput)
{
int counter;
for (counter = 2; counter < userinput; counter++) {
if (userinput % counter == 0)
return FALSE;
}
return TRUE;
}